﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using dll_ObjSerialization;

namespace CheckPermit
{
    /// <summary>
    /// 检查许可证,本地License使用明文加盐之后的二进制格式保存，
    /// 远程License使用HASH256编码
    /// </summary>
    public class Vm_CheckPermit
    {
        /// <summary>
        /// 验证是否得到许可
        /// </summary>
        /// <param name="licenseFileName"></param>
        /// <param name="remoteUrl"></param>
        /// <returns></returns>
        public static bool CheckPermit(string licenseFileName, string remoteUrl)
        {
            //检查License文件是否存在，不存在则返回false
            if(!File.Exists(licenseFileName))
            {
                return false;
            }

            //读取License文件
            Vm_License license = BinaryDeserializerHelper.DeSerializeBinary(licenseFileName) as Vm_License;

            //请求远程Liscense
            string strLicense = GetData(remoteUrl, Encoding.UTF8);

            //比较是否一致
            if (GetSHA256(license.Content) == strLicense)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 保存License文件
        /// </summary>
        /// <param name="licenseContent">License内容</param>
        /// <param name="licensePath">保存路径</param>
        public static void SaveLicense(string licenseContent, string licensePath)
        {
            Vm_License license = new Vm_License()
            {
                Content = licenseContent
            };

            BinaryDeserializerHelper.SerializeBinary(license, licensePath);
        }

        /// <summary>
        /// GET请求数据
        /// </summary>
        /// <param name="url">HTTP地址</param>
        /// <param name="encoder">编码类型</param>
        /// <returns></returns>
        public static string GetData(string url, Encoding encoder)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "text/html, application/xhtml+xml, */*";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream rs = response.GetResponseStream();
            StreamReader sr = new StreamReader(rs, encoder);
            var result = sr.ReadToEnd();
            sr.Close();
            rs.Close();
            return result;
        }

        /// <summary>
        /// 根据明文计算SHA256
        /// </summary>
        /// <param name="plaintext">明文</param>
        /// <returns></returns>
        public static string GetSHA256(string plaintext)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(plaintext);
            byte[] hash = SHA256Managed.Create().ComputeHash(bytes);

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                builder.Append(hash[i].ToString("x2"));
            }
            return builder.ToString();
        }

    }
}
